<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\mvc\request;

interface IHttp
{
  /**
   * Returns Header
   * 
   * @param type $sName
   */
  public function getHeader($sName);
}

class ContainerHttp extends Base implements IHttp
{
  const HEADER_ACCEPT = 'Accept';

  public function getCall()
  {
    $sUri = $this->getUri();
    $sPath = $this->getHeader('SCRIPT_NAME');

    //cut filename
    $sPath = \crcl\base\File::getInstance($sPath)->getDirectory();

    //normalize path
    $sPath = \str_replace('\\', '/', $sPath);
    $sPath = \crcl\base\String::getInstance($sPath)->doEndWith('/')->toString();

    //cut path from uri
    if (\strlen($sUri) > \strlen($sPath))
    {
      $sPath = \substr($sUri, \strlen($sPath));
    }
    else
    {
      $sPath = ''; //if uri and path are equal
    }

    // now we have the real uri

    //but we dont like the params
    $sPath = $this->cutParamsAndFragment($sPath);
    $sPath = $this->cutExtension($sPath);

    $sPath = \crcl\base\String::getInstance($sPath)
      ->doNotEndWith('/')
      ->doStartWith('/')
      ->toString();
    return (string)$sPath;
  }

  public function getMethod()
  {
    $sMethod = self::DEFAULT_METHOD;
    if (isset($_SERVER['REQUEST_METHOD']))
    {
      if ($_SERVER['REQUEST_METHOD'] != '')
      {
        $sMethod = $_SERVER['REQUEST_METHOD'];
      }
    }
    return $sMethod;
  }

  public function getView()
  {
    $sUri = $this->getUri();
    try
    {
      $sView = $this->cutParamsAndFragment($sUri);
      $sView = \crcl\base\File::getInstance($sView)->getExtension();            
    }
    catch (\crcl\base\file\Exception $e)
    {
      $sView = self::DEFAULT_VIEW;
    }
    return $sView;
  }

  public function hasXhtml()
  {
    $bResult = \FALSE;
    try
    {
      $sAccept = $this->getHeader('Accept');
      if (\strpos(\strtolower($sAccept), 'application/xhtml+xml') !== \FALSE)
      {
        $bResult = \TRUE;
      }
    }
    catch (\Exception $e)
    {
    }
    return $bResult;
  }

  public function getHeader($sName)
  {
    $sHeaderName = 'HTTP_' . \strtoupper(\str_replace('-', '_', $sName));
    if (isset($_SERVER[$sHeaderName]))
    {
      return $_SERVER[$sHeaderName];
    }
    else
    {
      if (isset($_SERVER[$sName]))
      {
        return $_SERVER[$sName];
      }
    }
    throw new Exception(
      'Invalid header',
      Exception::ERROR_HTTP_INVALIDHEADER,
      array('name' => $sName, '_SERVER' => $_SERVER));
  }

  /** PRIVATE *************************************************************************************/

  private function getUri()
  {
    try
    {
      $sUri = $this->getHeader('X_REWRITE_URL');
    }
    catch (Exception $e)
    {
      try
      {
        $sUri = $this->getHeader('REQUEST_URI');
      }
      catch (Exception $e)
      {
        $sUri = self::DEFAULT_CALL;
      }
    }
    return $sUri;
  }
}


